//
//  CookingModelViewController.m
//  OurMenu
//
//  Created by lanou3g on 15/10/12.
//  Copyright © 2015年 尹坤. All rights reserved.
//

#import "CookingModelViewController.h"
#import "UIImageView+WebCache.h"
static NSInteger a = 0;
@interface CookingModelViewController ()

@end

@implementation CookingModelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, self.view.frame.size.width - 140, 64)];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel setTextColor:[UIColor purpleColor]];
    titleLabel.text = self.dishs.title;
    self.navigationItem.titleView = titleLabel;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(jumpBack)];
    
    //backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 30, 60, 40);
//    backButton.backgroundColor = [UIColor redColor];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    [backButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(jumpBack) forControlEvents:UIControlEventTouchUpInside];
    #pragma mark - 配置步骤滑动视图
    stepScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(60, 0, self.view.frame.size.width - 60, self.view.frame.size.height)];
#pragma mark - 设置步骤滑动图背景颜色
    //stepScrollV.backgroundColor = [UIColor greenColor];
#warning 在这个地方用步骤数目代替滑动范围
    stepScrollV.contentSize = CGSizeMake(stepScrollV.frame.size.width, self.view.frame.size.height * self.dishs.steps.count  );
    stepScrollV.delegate = self;
    [self.view addSubview:stepScrollV];
    stepScrollV.bounces = NO;
    stepScrollV.pagingEnabled = YES;
    stepScrollV.showsVerticalScrollIndicator = NO;

    
    
    #pragma mark - 配置按钮滑动视图
    buttonScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 60, [[UIScreen mainScreen] bounds].size.height - 161)];
    buttonScrollV.showsVerticalScrollIndicator = NO;
#pragma mark - 设置按钮滑动视图背景颜色
    //buttonScrollV.backgroundColor = [UIColor cyanColor];
#warning 此处需要设置步骤数 + 1
    buttonScrollV.contentSize = CGSizeMake(60, 40*(self.dishs.steps.count)+ 10);
    [self.view addSubview:buttonScrollV];
#warning 此处需要设置步骤数 + 1
    for (int i = 1; i < self.dishs.steps.count + 1; i ++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        button.showsTouchWhenHighlighted = YES;
        button.tag = i;
        button.frame = CGRectMake(15, 10 + (i-1)*40, 30, 30);
//        if (i%2 == 0) {
//            button.backgroundColor = [UIColor greenColor];
//        }else {
//            button.backgroundColor = [UIColor redColor];
//        }
        button.backgroundColor = [UIColor colorWithRed:131.0/255 green:70.0/255 blue:45.0/255 alpha:0.85];
        [button addTarget:self action:@selector(markViewA:) forControlEvents:UIControlEventTouchUpInside];
        [buttonScrollV addSubview:button];
    }
    
    
    #pragma mark - 设置滚动视图上的展示页面
    for (int i = 1; i < self.dishs.steps.count + 1; i ++) {
        showView = [[UIView alloc] initWithFrame:CGRectMake(0, (i - 1) * self.view.frame.size.height, stepScrollV.frame.size.width, [[UIScreen mainScreen] bounds].size.height)];
#pragma mark - 设置颜色
        //showView.backgroundColor = [UIColor cyanColor];
        [stepScrollV addSubview:showView];
#warning 将数据放入数组中  根据元素下标和i对比放入数据
        UILabel *stepLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, stepScrollV.frame.size.width - 40, 120)];
        stepLabel.text = [self.dishs.steps[i - 1] valueForKey:@"step"];
        stepLabel.numberOfLines = 0;
        //stepLabel.backgroundColor = [UIColor blackColor];
        [showView addSubview:stepLabel];
        UIImageView *stepImageV = [[UIImageView alloc] initWithFrame:CGRectMake(30, 260, stepScrollV.frame.size.width - 60, 220)];
        
        [stepImageV sd_setImageWithURL:[NSURL URLWithString:[self.dishs.steps[i - 1] valueForKey:@"img"]] placeholderImage:[UIImage imageNamed:@"1"]];
        [showView addSubview:stepImageV];
        
    }
    
    
    
#pragma mark - 这是标记视图
    markView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 5, 30)];
    markView.backgroundColor = [UIColor orangeColor];
    [buttonScrollV addSubview:markView];
    
    
    #pragma mark - 设置小贴士和材料按钮
    
    UIButton *materilaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    materilaButton.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 163, 60, 100);
    materilaButton.layer.cornerRadius = 8;
#pragma mark - 设置按钮颜色
    
    materilaButton.backgroundColor = [UIColor colorWithRed:188.0/255 green:149.0/255 blue:99.0/255 alpha:0.85];
    materilaButton.showsTouchWhenHighlighted = YES;
    [materilaButton setTitle:@"用料" forState:UIControlStateNormal];
    [materilaButton addTarget:self action:@selector(touchMaterilaButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:materilaButton];
    
    #pragma mark - 材料视图
    materilaView = [[UIImageView alloc] initWithFrame:CGRectMake(buttonScrollV.frame.size.width, [[UIScreen mainScreen] bounds].size.height, stepScrollV.frame.size.width, 0)];
    materilaView.image = [UIImage imageNamed:@"CookBack.jpg"];
    //materilaView.backgroundColor = [UIColor colorWithRed:3/255.0 green:15/255.0 blue:112/255.0 alpha:0.3];
    materilaView.alpha = 0.95;
    materilaView.userInteractionEnabled = YES;
    [self.view addSubview:materilaView];
    
#pragma mark - 在材料视图上添加数据
    //材料标识
    ingredients = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 80, 40)];
    ingredients.font = [UIFont boldSystemFontOfSize:20];
    ingredients.textAlignment = NSTextAlignmentCenter;
    //ingredients.backgroundColor = [UIColor greenColor];
    ingredients.layer.borderColor = [UIColor colorWithRed:61.0/255 green:89.0/255 blue:271.0/255 alpha:1].CGColor;
    ingredients.layer.borderWidth = 0.5;
    ingredients.layer.cornerRadius = 5;
    ingredients.layer.masksToBounds = YES;
    ingredients.textColor = [UIColor colorWithRed:61.0/255 green:89.0/255 blue:171.0/255 alpha:1];
    ingredients.text = @"主料:";
    [materilaView addSubview:ingredients];
    //材料
    ingredientsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, materilaView.frame.size.width- 40, 80)];
    [ingredientsLabel setTextColor:[UIColor colorWithRed:208.0/255 green:49.0/255 blue:99.0/255 alpha:0.95]];
    ingredientsLabel.numberOfLines = 0;
    ingredientsLabel.text = self.dishs.ingredients;
    [materilaView addSubview:ingredientsLabel];
    //调味品标识
    burden = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 120, 40)];
    burden.font = [UIFont boldSystemFontOfSize:18];
    burden.textAlignment = NSTextAlignmentCenter;
    //burden.backgroundColor = [UIColor greenColor];
    burden.layer.borderColor = [UIColor colorWithRed:61.0/255 green:89.0/255 blue:271.0/255 alpha:1].CGColor;
    burden.layer.borderWidth = 0.5;
    burden.layer.cornerRadius = 5;
    burden.layer.masksToBounds = YES;
    burden.textColor = [UIColor colorWithRed:61.0/255 green:89.0/255 blue:171.0/255 alpha:1];
    burden.text = @"调味品和配料:";
    [materilaView addSubview:burden];
    //调味品
    burdenLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 270, materilaView.frame.size.width - 40, 320)];
    burdenLabel.font = [UIFont systemFontOfSize:16];
    AutoHeight_string *autoString = [[AutoHeight_string alloc] init];
    [burdenLabel sizeToFit];
    CGFloat ff = [autoString getRowHeight:16 width:210 content:self.dishs.burden];
    [burdenLabel setFrame:CGRectMake(20, 270, materilaView.frame.size.width - 40, ff)];
    [burdenLabel setTextColor:[UIColor colorWithRed:208.0/255 green:49.0/255 blue:99.0/255 alpha:0.95]];
    //burdenLabel.backgroundColor = [UIColor greenColor];
    burdenLabel.numberOfLines = 0;
    burdenLabel.text = self.dishs.burden;
    [materilaView addSubview:burdenLabel];
    #pragma mark - 材料视图关闭按钮
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    closeButton.frame = CGRectMake(materilaView.frame.size.width - 30, 0, 30,30);
    closeButton.backgroundColor = [UIColor colorWithRed:331.0/255 green:70.0/255 blue:45.0/255 alpha:0.85];
    [closeButton setTitle:@"X" forState:UIControlStateNormal];
    closeButton.layer.cornerRadius = 10;
    closeButton.showsTouchWhenHighlighted = YES;
    [closeButton addTarget:self action:@selector(closeMaterilaView) forControlEvents:UIControlEventTouchUpInside];
    [materilaView addSubview:closeButton];
    
    // Do any additional setup after loading the view.
}

#pragma mark - 返回点击方法
- (void)jumpBack
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


//点击button按钮标记视图动画方法
-(void)markViewA:(UIButton *)button
{
    [UIView animateWithDuration:0.2 animations:^{
        [markView setFrame:CGRectMake(button.frame.origin.x, button.frame.origin.y, 5, 30)];
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
            stepScrollV.contentOffset = CGPointMake(0, stepScrollV.frame.size.height * (button.tag -1));
        //NSLog(@"--------%ld",button.tag - 1);
    }];

}


#pragma mark - UIScrollView的代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取scrollView偏移量
    CGPoint point = scrollView.contentOffset;
    //根据偏移量计算滑动到了第几页
    NSInteger number = point.y / self.view.frame.size.height;
    //设置标记视图位置
    if (number > 10) {
        number = number - 1;
    }
    //NSLog(@"%ld",number);
    [UIView animateWithDuration:0.2 animations:^{
        
        [markView setFrame:CGRectMake(10, 10 + (number * 40) , 5, 30)];
    }];
    
}

//点击用料按钮touchMaterilaButton方法
- (void)touchMaterilaButton
{
    
    if (a == 0) {
        [UIView animateWithDuration:0.3 animations:^{
                        [materilaView setFrame:CGRectMake(buttonScrollV.frame.size.width, 0, stepScrollV.frame.size.width, [[UIScreen mainScreen] bounds].size.height - 64)];
            [stepScrollV setAlpha:0.1];
            [buttonScrollV setAlpha:0.1];
        }];

        a++;
    }else if (a == 1) {
        [UIView animateWithDuration:0.3 animations:^{
                    [materilaView setFrame:CGRectMake(buttonScrollV.frame.size.width, [[UIScreen mainScreen] bounds].size.height, stepScrollV.frame.size.width, 0)];
            [stepScrollV setAlpha:1];
            [buttonScrollV setAlpha:1];
        }];

        a--;
    }
        

}

-(void)closeMaterilaView
{
    [UIView animateWithDuration:0.3 animations:^{
        [materilaView setFrame:CGRectMake(buttonScrollV.frame.size.width, [[UIScreen mainScreen] bounds].size.height, stepScrollV.frame.size.width, 0)];
        [stepScrollV setAlpha:1];
        [buttonScrollV setAlpha:1];
    }];
    
    a--;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
